package com.senjoeson.wanandroid;


import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.senjoeson.wanandroid.adapter.FragmentVPAdapter;
import com.senjoeson.wanandroid.base.BaseActivity;
import com.senjoeson.wanandroid.ui.fragment.HomeFragment;
import com.senjoeson.wanandroid.ui.fragment.MyFragment;
import com.senjoeson.wanandroid.ui.fragment.SystemFragment;

import java.util.ArrayList;

import butterknife.BindView;


public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.bottomNavigation)
    BottomNavigationView mBottomNavigation;
    @BindView(R.id.fab)
    FloatingActionButton mFab;
    @BindView(R.id.left_navigation)
    NavigationView mLeftNavigation;
    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;


    public ViewPager.OnPageChangeListener mViewPagerOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    mToolbar.setTitle(R.string.title_home);
                    break;
                case 1:
                    mToolbar.setTitle(R.string.title_dashboard);
                    break;
                case 2:
                    mToolbar.setTitle(R.string.title_notifications);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };



    @Override
    protected int getLayoutResource() {
        return R.layout.activity_main;
    }

    @Override
    protected void onInitView() {
        setSupportActionBar(mToolbar);
        initLeftNavigation();
        mLeftNavigation.setNavigationItemSelectedListener(this);
        FragmentVPAdapter fragmentVPAdapter = new FragmentVPAdapter(getSupportFragmentManager());
        fragmentVPAdapter.setFragments(getFragments());
        mViewpager.setAdapter(fragmentVPAdapter);
        mViewpager.addOnPageChangeListener(mViewPagerOnPageChangeListener);
        mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        mViewpager.setCurrentItem(0);
                        mToolbar.setTitle(R.string.title_home);
                        return true;
                    case R.id.navigation_dashboard:
                        mViewpager.setCurrentItem(1);
                        mToolbar.setTitle(R.string.title_dashboard);
                        return true;
                    case R.id.navigation_notifications:
                        mViewpager.setCurrentItem(2);
                        mToolbar.setTitle(R.string.title_notifications);
                        return true;
                }
                return false;
            }
        });
    }

    private void initLeftNavigation() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawerLayout.addDrawerListener(toggle);
        toggle.syncState();
    }

    /**
     * @return 返回一个fragment集合
     */
    public ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new SystemFragment());
        fragments.add(new MyFragment());
        return fragments;
    }





    @Override
    public void onBackPressed() {
        DrawerLayout drawer =  findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        switch (item.getItemId()) {
            case R.id.nav_camera:
                // Handle the camera action
                break;
            case R.id.nav_gallery:

                break;
            case R.id.nav_slideshow:

                break;
            case R.id.nav_manage:

                break;
            case R.id.nav_share:

                break;
            case R.id.nav_send:

                break;
        }
        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}
